#include <iostream>
#include <stdio.h>
#include <iterator> 
#include <string>
#include <list>
#include <utility>
#include <algorithm>
#include <string.h>
using namespace std;
typedef unsigned short ushort;
ushort RAM[1000][3];
ushort REG[10];
typedef pair<ushort, ushort> programme_counter;
list<programme_counter> programme_table;
#define clear_register() memset(REG,0, 20);
#define init_ram() memset(RAM , 0, 6000);
#define INS(i) RAM[i][0]
#define ARG1(i) RAM[i][1]
#define ARG2(i) RAM[i][2]
#define OFFSET _PC_.first

// Need tp put check for pc.first overwrite
// Second time pagefault will overwrite the 
// pc.first
bool load_ram( programme_counter & pc) {
	string statement("");
	cin >> noskipws;
	//while(pc.second < pc.first) { // not tested
	while(pc.second < 1000) {
		statement.clear();
		getline(cin, statement);
		if(statement.size() < 3) {
			if(cin.eof() == true) {
				programme_table.push_back(pc);
				goto _EOF_;
				break;
			} else {
				pc.second--;
				programme_table.push_back(pc);
				pc.second++;
				pc.first = pc.second;
				statement.clear();
				while(statement.size() < 3)
					getline(cin, statement);
			}
		}
		//cout << statement << "; " << pc.second << endl;
		RAM[pc.second][0] = statement[0]-48;
		RAM[pc.second][1] = statement[1]-48;
		RAM[pc.second][2] = statement[2]-48;
		pc.second++;
	}
	programme_table.push_back(pc);
	return true;
_EOF_:
	return false;
}

void print_ram() {
	for(ushort i = 0; i < 1000; i++) {
		cout << RAM[i][0] << RAM[i][1] << RAM[i][2] << endl;
	}
}

/* 100 means halt
   2dn means set register d to n (between 0 and 9)
   3dn means add n to register d
   4dn means multiply register d by n
   5ds means set register d to the value of register s
   6ds means add the value of register s to register d
   7ds means multiply register d by the value of register s
   8da means set register d to the value in RAM whose address is in register a
   9sa means set the value in RAM whose address is in register a to the value of register s
   0ds means goto the location in register d unless register s contains 0
   */

ushort execute (const programme_counter& _PC_) {
	ushort pc = OFFSET;
	ushort counter = 0;
	while( true ) {
		//cout << "pc:" << pc << " Ins:" << INS(pc) << endl;
		switch(INS(pc)) {
			case 0:
				if(REG[ARG2(pc)] != 0) {
					pc = OFFSET + REG[ARG1(pc)];
				} else {
					pc++;	
				}
				counter += 1;
				break;
			case 1:
				counter +=1;
				goto DONE;
				break;
			case 2:
				REG[ARG1(pc)] = ARG2(pc);
				pc++;
				counter += 1;
				break;
			case 3:
				REG[ARG1(pc)] = (REG[ARG1(pc)] + ARG2(pc)) % 1000;
				pc++;
				counter += 1;
				break;
			case 4:
				REG[ARG1(pc)] = (REG[ARG1(pc)] * ARG2(pc)) % 1000;
				pc++;
				counter += 1;
				break;
			case 5:
				REG[ARG1(pc)] = REG[ARG2(pc)];
				pc++;
				counter += 1;
				break;
			case 6:
				REG[ARG1(pc)] = (REG[ARG1(pc)] + REG[ARG2(pc)]) % 1000;
				pc++;
				counter += 1;
				break;
			case 7:
				REG[ARG1(pc)] = (REG[ARG1(pc)] * REG[ARG2(pc)]) % 1000;
				pc++;
				counter += 1;
				break;
			case 8:
				REG[ARG1(pc)] =  RAM[(OFFSET+REG[ARG2(pc)])%1000][2]+\
								(RAM[(OFFSET+REG[ARG2(pc)])%1000][1] << 3) +\
								(RAM[(OFFSET+REG[ARG2(pc)])%1000][1] << 1)+ \
								(RAM[(OFFSET+REG[ARG2(pc)])%1000][0] << 6) +\
								(RAM[(OFFSET+REG[ARG2(pc)])%1000][0] << 5) + \
								(RAM[(OFFSET+REG[ARG2(pc)])%1000][0] << 2);
				pc++;
				counter += 1;
				break;
			case 9:
				RAM[(OFFSET+REG[ARG2(pc)])%1000][2] = REG[ARG1(pc)]%10;
				RAM[(OFFSET+REG[ARG2(pc)])%1000][1] = (REG[ARG1(pc)]/10)%10;
				RAM[(OFFSET+REG[ARG2(pc)])%1000][0] = REG[ARG1(pc)]/100;
				pc++;
				counter += 1;
				break;
			default:
				break;
		}
		if(pc > OFFSET && pc > _PC_.second)
			goto DONE;
		pc %= 1000;
	}
DONE:
	return counter;
}

/* main
 *  * */
int main() {
	bool exit = false;
	long no_of_programmes = 0;
	bool pagefault = false;
	cin >> no_of_programmes;
	cin.ignore(); // changeLine
	cin >> noskipws;
	string statement;
	getline(cin, statement); // Blank line
	getline(cin, statement); // Blank line
	//cin.ignore(); // changeLine
	//init_ram();
	programme_counter pc(0, 0);
AGAIN:	
	pagefault = load_ram(pc);
	if(pagefault == true) {
		pc = programme_table.back();
		pc.second = (pc.second % 1000);
		programme_table.pop_back();
		//cout << "Pagefault:" << pc.second << endl;
	}
	while(programme_table.empty() == false){
		clear_register();
		//cout << programme_table.front().first << ", " << programme_table.front().second <<endl;
		cout << execute(programme_table.front()) <<endl;
		programme_table.pop_front();
		if(no_of_programmes > 1)
			cout << endl;
		no_of_programmes--;
	}
	if(pagefault == true)
		goto AGAIN;

	//print_ram();
	return 0;
}
